package com.in.ussd.database;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.in.ussd.constant.ConstantVariables;
import com.in.ussd.model.UssdAdapterModel;

public class UssdDatasource {

	private SQLiteDatabase database;
	private MyDatabaseHelper dbHelper;

	public UssdDatasource(Context context) {

		// Copy database from asset folder
		File dbFile = context.getDatabasePath(ConstantVariables.DATABASE_NAME);

		if (!dbFile.exists()) {
			copyDatabase(context);
		}

		dbHelper = new MyDatabaseHelper(context);
	}

	public void open() {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {

		dbHelper.close();
	}

	public List<UssdAdapterModel> getSpinCodesInfo() {
		List<UssdAdapterModel> codes = new ArrayList<UssdAdapterModel>();

		String sqlStr = "SELECT DISTINCT " + ConstantVariables.COLUMN_USSD_ID
				+ ", TRIM(" + ConstantVariables.COLUMN_USSD_CODE_DETAIL + ") AS "
				+ ConstantVariables.COLUMN_USSD_CODE_DETAIL + " FROM "
				+ ConstantVariables.TABLE_USSD_INFORMATION + ";";
		Cursor cursor = database.rawQuery(sqlStr, null);

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				Integer _id = cursor.getInt(cursor
						.getColumnIndex(ConstantVariables.COLUMN_USSD_ID));
				String ussdCodeDetail = cursor.getString(cursor
						.getColumnIndex(ConstantVariables.COLUMN_USSD_CODE_DETAIL));
				UssdAdapterModel model = new UssdAdapterModel();
				model.setUssdId(_id);
				model.setUssdDetail(ussdCodeDetail);
				codes.add(model);
				cursor.moveToNext();
			}
		}

		cursor.close();
		return codes;
	}

	public List<UssdAdapterModel> getUssdInformation(Integer _id) {
		List<UssdAdapterModel> ussds = new ArrayList<UssdAdapterModel>();

		String sqlStr = "SELECT * FROM "
				+ ConstantVariables.TABLE_USSD_INFORMATION + " WHERE "
				+ ConstantVariables.COLUMN_USSD_ID + " = " + _id + ";";

		Cursor cursor = database.rawQuery(sqlStr, null);

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {

				UssdAdapterModel ussdModel = new UssdAdapterModel();
				ussdModel
						.setUssdCode(cursor
								.getString(
										cursor.getColumnIndex(ConstantVariables.COLUMN_USSD_CODE))
								.trim());
				ussdModel
						.setUssdDetail(cursor
								.getString(
										cursor.getColumnIndex(ConstantVariables.COLUMN_USSD_CODE_DETAIL))
								.trim());

				ussds.add(ussdModel);

				cursor.moveToNext();
			}
		}
		cursor.close();
		return ussds;
	}

	private void copyDatabase(Context context) {
		InputStream mInput;
		try {
			mInput = context.getAssets().open(ConstantVariables.DATABASE_NAME);
			String outFileName = "data/data/" + context.getPackageName()
					+ "/databases/" + ConstantVariables.DATABASE_NAME;
			File mfile = new File(outFileName);
			if (!mfile.exists()) {
				File dir = new File("data/data/" + context.getPackageName()
						+ "/databases/");
				dir.mkdir();
				mfile.createNewFile();
			}

			OutputStream mOutput = new FileOutputStream(outFileName);
			byte[] buffer = new byte[1024];
			int length;
			while ((length = mInput.read(buffer)) > 0) {
				mOutput.write(buffer, 0, length);
			}

			mOutput.flush();
			mOutput.close();
			mInput.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}
